var screenId = "bb-item1";
var isFirst = true;

function getAjaxRequestObject()
{
	if (window.XMLHttpRequest)
		return (new XMLHttpRequest());
	else if (window.ActiveXObject)
		return (new ActiveXObject("Microsoft.XMLHTTP"));
	else
		return(null);
}

function sendRequest(latitude, longitude)
{
	var request = getAjaxRequestObject();
	request.onreadystatechange = function() {
		responseCallback(request);
	};
	request.open("GET", "Dispatcher?longitude=" + longitude + "&latitude=" + latitude, true);
	request.send(null);
}
var keywordIndex = 0;
var keywordArray;

function responseCallback(request)
{
	if (request.readyState == 4)
	{	
		var ele = document.getElementById(screenId);
		keywordArray = request.responseText.split("<br>");
		ele.innerHTML = "";
		for (var i = 0 ; i < keywordArray.length ; ++i)
		{
			var idx = i + 1;
			ele.innerHTML += (idx + ". " + keywordArray[i] + "<br>");
		}
	
		SearchGoogle(keywordArray[keywordIndex]);
		ele.innerHTML = request.responseText;
		
//		if (screenId == "bb-item1")
//		{
//			screenId = "bb-item2";
//		}
//		else
//		{
//			screenId = "bb-item1";
//		}
		
//		if (isFirst == true)
//		{
//			sendRequest(latitude, longitude);
//			isFirst = false;
//		}
		// TODO: insert keyword search result.
		
		// search to google API.
	}
	// TODO: re-request to server if needed.
	
}

function getGeolocation()
{
	navigator.geolocation.getCurrentPosition(Success, Failure);
}

function Success(pos)
{	
	var latitude = pos.coords.latitude;
	var longitude = pos.coords.longitude;
	
	map = new daum.maps.Map(document.getElementById('map_canvas'), {
		center: new daum.maps.LatLng(latitude, longitude),
		level: 3
	});
	var zoomControl = new daum.maps.ZoomControl();
	map.addControl(zoomControl, daum.maps.ControlPosition.RIGHT);
	var mapTypeControl = new daum.maps.MapTypeControl();
	map.addControl(mapTypeControl, daum.maps.ControlPosition.TOPRIGHT);

	daum.maps.event.addListener(map,"dragend",function(){
		var center = map.getCenter();
		var latitude = center.getLat();
		var longitude = center.getLng();
		
		sendRequest(latitude, longitude);
	});
	
	sendRequest(latitude, longitude);
};

function Failure()
{
	// Fail to get geolocation.
}


google.load('search', '1');

var cursor = 0;
var firstCall = true;

function SearchGoogle( keyword )
{
	var searchControl = new google.search.SearchControl();

	searchControl.addSearcher(new google.search.NewsSearch());
    searchControl.draw(document.getElementById("abc"));
    searchControl.execute( keyword );
    
    searchControl.setSearchCompleteCallback(this, OnSearchComplete = function(sc, searcher)
  		{
    		var str = 'news-item' + keywordIndex;
	  		var div = document.getElementById(str);
	  		
  	  		if ( searcher.results && searcher.results.length > 0)
  	  		{
  		    	for (var i = 0; i < searcher.results.length; ++i)
  		    	{
  		    		var a = document.getElementById(str + i);
  		    		if (a == null)
  		    			a = document.createElement('a');
  		    		
  		    		a.href = decodeURIComponent(searcher.results[i].url);
  		    		a.innerHTML = searcher.results[i].title + "<br><br>";
  		    		a.target = '_blank';
  		    		a.setAttribute("id", str + i);
  		    		div.appendChild(a);
  		    	}
  	  		}
  	  		else
  	  		{
  	  			div.innerHTML = "There is no News";
  	  		}
  	  		keywordIndex++;
  	  		var len;
  	  		if (keywordArray.length > 10)
  	  			len = 10;
  	  		else
  	  			len = keywordArray.length;
  	  		if (keywordIndex < len)
  	  			SearchGoogle(keywordArray[keywordIndex]);
  	  		else
  	  			keywordIndex = 0;
    		});
}


